<!DOCTYPE html>
<html lang="en" xmlns:sa="http://www.thymeleaf.org/extras/sa-token"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--公共模块-->
    <div th:replace="~{layout}"></div>
</head>
<body>
<div id="searchParam">
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item" sa:hasPermission="sys:log:list">
            <div class="layui-input-inline">
                <input type="text" name="username" class="layui-input" autocomplete="off" placeholder="请输入账号">
            </div>
            <div class="layui-input-inline">
                <input type="text" name="operation" class="layui-input" autocomplete="off" placeholder="用户操作动作">
            </div>
            <div class="layui-input-inline">
                <input type="text" readonly class="layui-input" name="createTimeBegin" id="createTimeBegin" placeholder="开始时间">
            </div>
            <div class="layui-input-inline">
                <input type="text" readonly class="layui-input" name="createTimeEnd" id="createTimeEnd" placeholder="结束时间">
            </div>
            <div class="layui-input-inline ">
                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
            </div>
        </div>
    </form>
</div>
<table class="layui-hide" id="log_table" lay-filter="log_table"></table>
<div id="laypage"></div>
</body>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button sa:hasPermission="sys:log:deleted" type="button" class="layui-btn layui-btn-sm"
                lay-event="getCheckData">
            批量删除
        </button>
    </div>
</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" sa:hasPermission="sys:log:deleted">删除</a>
</script>
<script>
    //查询条件
    layui.use(function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;

        //加载table
        var tableIns1 = table.render({
            elem: '#log_table'
            , contentType: 'application/json'
            , page: true //开启分页
            , url: ctx + 'sys/logs' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'username', title: '账号', width: 130},
                    {field: 'operation', title: '用户操作', width: 140},
                    {field: 'method', title: '请求方法', width: 400},
                    {field: 'params', title: '请求参数', width: 300},
                    {field: 'createTime', title: '创建时间', minWidth: 120},
                    {width: 150, toolbar: "#tool", title: '操作'}
                ]
            ]
            , toolbar: '#toolbar'
        });


        //日期范围
        laydate.render({
            elem: '#createTimeBegin'
            , type: 'datetime'
        });
        //日期范围
        laydate.render({
            elem: '#createTimeEnd'
            , type: 'datetime'
        });

        //表头工具栏事件
        table.on('toolbar(log_table)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量删除的日志");
                    } else {
                        var logIds = [];
                        $(data).each(function (index, item) {
                            logIds.push(item.id);
                        });
                        tipDialog(logIds);
                    }
                    break;
            }
            ;
        });
        //操作工具栏事件
        table.on('tool(log_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var logIds = [];
                    logIds.push(data.id);
                    tipDialog(logIds);
                    break;
            }
        });

        //删除前确认对话框
        var tipDialog = function (logIds) {
            layer.open({
                content: '确定要删除么',
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete(ctx + "/logs", logIds, function (res) {
                        layer.msg(res.msg);
                        tableIns1.reload();
                    });
                }
            });
        }


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            tableIns1.reload({
                page: {
                    curr: 1
                }
                , where: data.field
            });
            return false;
        });
    });


</script>
</html>